package com.grandbroad.eim.adapter;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.easy.west.westclient.api.base.bean.VodBean;
import com.grandbroad.eim.R;
import com.grandbroad.eim.entry.ClassIconBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ClassPageAdaapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int CLIASS_ITEM = 0;
    public static final int HOT_ITEM = 1;
    private List<ClassIconBean> classIconBeanList = new ArrayList<>();
    private IconListAdapter listAdapter;
    private List<VodBean> hotlist = new ArrayList<>();
    private VodListAdpter vodListAdpter;
    public ClassPageAdaapter(AppCompatActivity appCompatActivity)
    {
        vodListAdpter = new VodListAdpter(appCompatActivity);
        listAdapter = new IconListAdapter();
        //初始化分类
        ClassIconBean iconBean_1 = new ClassIconBean();
        iconBean_1.setId(1);
        iconBean_1.setTag("云计算");
        iconBean_1.setRes(R.drawable.icon_class_cloud);
        classIconBeanList.add(iconBean_1);
        ClassIconBean iconBean_2 = new ClassIconBean();
        iconBean_2.setId(2);
        iconBean_2.setTag("物联网");
        iconBean_2.setRes(R.drawable.icon_class_internet);
        classIconBeanList.add(iconBean_2);
        ClassIconBean iconBean_3 = new ClassIconBean();
        iconBean_3.setId(3);
        iconBean_3.setTag("大数据");
        iconBean_3.setRes(R.drawable.icon_class_bigdata);
        classIconBeanList.add(iconBean_3);
        ClassIconBean iconBean_4 = new ClassIconBean();
        iconBean_4.setId(4);
        iconBean_4.setTag("人工智能");
        iconBean_4.setRes(R.drawable.icon_class_ai);
        classIconBeanList.add(iconBean_4);
        ClassIconBean iconBean_5 = new ClassIconBean();
        iconBean_5.setId(5);
        iconBean_5.setTag("区块链");
        iconBean_5.setRes(R.drawable.icon_class_blockchain);
        classIconBeanList.add(iconBean_5);
        ClassIconBean iconBean_6 = new ClassIconBean();
        iconBean_6.setId(6);
        iconBean_6.setRes(R.drawable.icon_class_other);
        iconBean_6.setTag("其他");
        classIconBeanList.add(iconBean_6);


    }
    @Override
    public int getItemViewType(int position) {
        if (position == CLIASS_ITEM) {
            return CLIASS_ITEM;
        } else {
            return HOT_ITEM;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = null;
        if (getItemViewType(i) == CLIASS_ITEM) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.class_iconlist_item, viewGroup, false);
            return new ClassIconsViewHolder(view);
        } else if (getItemViewType(i) == HOT_ITEM) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.hot_class_list, viewGroup, false);
            return new HotItemViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof ClassIconsViewHolder) {
            ClassIconsViewHolder iconsViewHolder = (ClassIconsViewHolder) viewHolder;
            iconsViewHolder.recyclerClassIconsList.setLayoutManager(new GridLayoutManager(iconsViewHolder.itemView.getContext(),3,GridLayoutManager.VERTICAL,false));
            iconsViewHolder.recyclerClassIconsList.setAdapter(listAdapter);
            listAdapter.setClassIconBeanList(classIconBeanList);
            listAdapter.notifyDataSetChanged();
        }
        if (viewHolder instanceof HotItemViewHolder) {
            HotItemViewHolder itemViewHolder = (HotItemViewHolder) viewHolder;
            if (hotlist.size()>0)
            {
                itemViewHolder.recyclerHotList.setLayoutManager(new LinearLayoutManager(viewHolder.itemView.getContext()));
                itemViewHolder.recyclerHotList.setAdapter(vodListAdpter);
                vodListAdpter.setVodList(hotlist);
                vodListAdpter.notifyDataSetChanged();
            }

        }

    }

    @Override
    public int getItemCount() {
        return 2;
    }

    public void setHotlist(List<VodBean> hotlist) {
        this.hotlist = hotlist;
    }



    /**
     * 列表图标适配器
     */
    class ClassIconsViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.recycler_class_icons_list)
        RecyclerView recyclerClassIconsList;

        public ClassIconsViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    /**
     * 热门列表适配器
     */
    class HotItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.recycler_hot_list)
        RecyclerView recyclerHotList;
        public HotItemViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
